package cn.com.surker.material.service.impl;

import cn.com.surker.dto.main.MainSupplierQueryDTO;
import cn.com.surker.dto.main.MainSupplierUpdateDTO;
import cn.com.surker.material.dao.MainSupplierDao;
import cn.com.surker.material.entity.MainSupplier;
import cn.com.surker.material.service.MainSupplierService;
import cn.com.surker.oa.dao.OaSupplierDao;
import cn.com.surker.oa.entity.OaSupplier;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.vo.main.MainSupplierVO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.snaker.engine.SnakerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MainSupplierServiceImpl extends ServiceImpl<MainSupplierDao, MainSupplier> implements MainSupplierService {


    @Resource
    private MainSupplierDao dao;
    @Resource
    private OaSupplierDao supplierDao;

    @Override
    public IPage<MainSupplierVO> pageData(MainSupplierQueryDTO dto) {
        PageHelper.offsetPage(dto.getOffsetSize(), dto.getLimit());
        IPage<MainSupplierVO> supplierPage = new Page<>();
        QueryWrapper<MainSupplier> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(dto.getSearchVal())) {
            queryWrapper.and(qwr -> qwr.like("name", dto.getSearchVal()).or().like("full_name", dto.getSearchVal()).or().like("legal", dto.getSearchVal()).or().like("phone", dto.getSearchVal()).or().like("link_man", dto.getSearchVal()).or().like("tax_code", dto.getSearchVal()).or().like("address", dto.getSearchVal()).or().like("remark", dto.getSearchVal()));
        }
        if (ObjectUtil.isNotEmpty(dto.getOrgId())) {
            queryWrapper.eq("org_id", dto.getOrgId());
        }
        if (ObjectUtil.isNotEmpty(dto.getType())) {
            queryWrapper.eq("type", dto.getType());
        }
        if (ObjectUtil.isNotEmpty(dto.getStatus())) {
            queryWrapper.eq("status", dto.getStatus());
        }
        IPage<MainSupplier> mainSupplierPage = dao.selectPage(new Page<>(dto.getCurrSize(), dto.getLimit()), queryWrapper);
        if (ObjectUtil.isNotEmpty(mainSupplierPage)) {
            supplierPage.setTotal(mainSupplierPage.getTotal());
            supplierPage.setPages(mainSupplierPage.getPages());
            supplierPage.setCurrent(mainSupplierPage.getCurrent());
            supplierPage.setSize(mainSupplierPage.getSize());
            if (CollUtil.isNotEmpty(mainSupplierPage.getRecords())) {
                PageInfo pageInfo = new PageInfo(mainSupplierPage.getRecords());
                if (ObjectUtil.isNotEmpty(pageInfo.getTotal())){
                    supplierPage.setTotal(pageInfo.getTotal());
                }
                List<MainSupplierVO> listVo = new ArrayList<>();
                for (MainSupplier mainSupplier : mainSupplierPage.getRecords()) {
                    MainSupplierVO mainSupplierVO = BeanUtil.copyProperties(mainSupplier, MainSupplierVO.class);
                    listVo.add(mainSupplierVO);
                }
                supplierPage.setRecords(listVo);
            }
        }
        return supplierPage;
    }


    @Override
    public MainSupplierVO queryDetail(Long id) {
        MainSupplier mainSupplier = dao.selectById(id);
        if (ObjectUtil.isEmpty(mainSupplier)) {
            throw new SnakerException("当前供应商不存在，请重新处理！");
        }
        MainSupplierVO mainSupplierVO = BeanUtil.copyProperties(mainSupplier, MainSupplierVO.class);
        return mainSupplierVO;
    }

    @Override
    public Boolean updateMainSpupplier(MainSupplierUpdateDTO dto, CacheUser user) {
        if (ObjectUtil.isEmpty(dto.getId())) {
            throw new SnakerException("标识不存在，请检查数据！");
        }
        MainSupplier mainSupplier = dao.selectById(dto.getId());
        if (ObjectUtil.isEmpty(mainSupplier)) {
            throw new SnakerException("当前供应商不存在，请重新处理！");
        }
        BeanUtil.copyProperties(dto, mainSupplier);
        mainSupplier.setUpdateTime(new Date());
        mainSupplier.setUpdator(user.getName());
        return updateById(mainSupplier);
    }

    @Override
    public Boolean updatestatus(MainSupplierUpdateDTO dto, CacheUser user) {
        if (ObjectUtil.isEmpty(dto.getId())) {
            throw new SnakerException("标识不存在，请检查数据！");
        }
        MainSupplier mainSupplier = dao.selectById(dto.getId());
        if (ObjectUtil.isEmpty(mainSupplier)) {
            throw new SnakerException("当前供应商不存在，请重新处理！");
        }
        mainSupplier.setStatus(dto.getStatus());
        mainSupplier.setUpdateTime(new Date());
        mainSupplier.setUpdator(user.getName());
        return updateById(mainSupplier);
    }

    @Override
    public List<MainSupplierVO> listByOrgId(MainSupplierQueryDTO dto) {
        List<MainSupplierVO> listVo = new ArrayList<>();
        QueryWrapper<MainSupplier> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(dto.getOrgId())){
            queryWrapper.eq("org_id",dto.getOrgId());
        }
        List<MainSupplier> listMain = dao.selectList(queryWrapper);
        if(CollUtil.isNotEmpty(listMain)){
            for (MainSupplier mainSupplier : listMain) {
                MainSupplierVO mainSupplierVO = BeanUtil.copyProperties(mainSupplier, MainSupplierVO.class);
                listVo.add(mainSupplierVO);
            }
        }
        return listVo;
    }

    /**
     * 根据公司ID查询供应商
     * @param orgId
     * @return
     */
    public List<OaSupplier> findSupplierByOrg(Long orgId) {
        QueryWrapper<OaSupplier> supplierQuery = new QueryWrapper<>();
        supplierQuery.eq("status", 0);
        if (orgId != null)
            supplierQuery.inSql("id", "select supplier_id from oa_supplier_org " +
                    "where status = 0 and type in (1,2,3) and org_id = " + orgId);
        List<OaSupplier> list = supplierDao.selectList(supplierQuery);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }


}
